package com.example.crm.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.example.crm.pojo.Link;
import com.example.crm.utils.JDBCUtils;

public class LinkDAO {

	public int insert(Link l) throws SQLException {
		// 1. 获取连接
		Connection conn = JDBCUtils.getConnection();

		// 2. 创建Statment
		String sql = "insert into link_man values (null,?,?,?,?)";// (需要修改)
		PreparedStatement stmt = conn.prepareStatement(sql);

		// (需要修改)
		stmt.setString(1, l.getName());
		stmt.setInt(2, l.getLinkId());
		stmt.setInt(3, l.getCreateId());
		stmt.setString(4, l.getTel());


		// 3. 执行层语句
		int result = stmt.executeUpdate();

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return result;
	}

	public int deleteById(int id) throws SQLException {
		// 1. 获取连接
		Connection conn = JDBCUtils.getConnection();

		// 2. 创建Statment
		String sql = "delete from link_man where link_id=?";// (需要修改)
		PreparedStatement stmt = conn.prepareStatement(sql);

		// (需要修改)
		stmt.setInt(1, id);

		// 3. 执行层语句
		int result = stmt.executeUpdate();

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return result;
	}

	public int update(Link l) throws SQLException {
		// 1. 获取连接
		Connection conn = JDBCUtils.getConnection();

		// 2. 创建Statment
		String sql = "update link_man set "
				+ "link_name=?,"
				+ "link_tel=?"
				+ " where link_id=?";// (需要修改)
		PreparedStatement stmt = conn.prepareStatement(sql);

		// (需要修改)
		stmt.setString(1, l.getName());
		stmt.setString(2, l.getTel());
	
		


		// 3. 执行层语句
		int result = stmt.executeUpdate();

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return result;
	}

	public Link findById(int id) throws SQLException {
		// 1. 获取连接
		Connection conn = JDBCUtils.getConnection();

		// 2. 创建Statment
		String sql = "select * from link_man where link_id=?";// (需要修改)
		PreparedStatement stmt = conn.prepareStatement(sql);

		stmt.setInt(1, id);

		// 3. 执行层语句
		ResultSet rs = stmt.executeQuery();
		Link l = null;
		if (rs.next()) {
			String name = rs.getString("link_name");
			int linkId = rs.getInt("link_link_id");
			int createId = rs.getInt("link_create_id");
			String tel = rs.getString("link_tel");


			l= new Link(id, name, tel, createId,linkId);

		}

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return l;
	}

	public List<Link> findAll() throws SQLException {
		// 1. 获取连接
		Connection conn = JDBCUtils.getConnection();

		// 2. 创建Statment
		String sql = "select * from link_man";// (需要修改)
		PreparedStatement stmt = conn.prepareStatement(sql);

		// 3. 执行层语句
		ResultSet rs = stmt.executeQuery();
		List<Link> list = new ArrayList<>();
		while (rs.next()) {
			int id = rs.getInt("link_id");
			String name = rs.getString("link_name");
			int userId = rs.getInt("link_user_id");
			int createId = rs.getInt("link_create_id");
			String tel = rs.getString("link_tel");
			

			Link l = new Link(id, name, linkId, createId, tel);
			list.add(l);
		}

		// 4. 释放资源
		JDBCUtils.destory(conn, stmt, null);

		return list;
	}
}